package fu.capstone.hanoicityguide.presentation.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.model.Cyclo;

public class ListCycloAdapter extends ArrayAdapter<Cyclo> {

	private Activity context = null;
	private int resource;
	private ArrayList<Cyclo> arr = null;

	// variable for imageloader
	private DisplayImageOptions options;
	private ImageLoader imageLoader;

	public ListCycloAdapter(Activity context, int resource,
			ArrayList<Cyclo> objects) {
		super(context, resource, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.resource = resource;
		this.arr = objects;
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				getContext()).build();
		if (!ImageLoader.getInstance().isInited()) {
			ImageLoader.getInstance().init(config);
		}
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_photos)
				.showImageForEmptyUri(R.drawable.ic_photos)
				.showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
				.cacheOnDisk(true).considerExifParams(true)
				.displayer(new RoundedBitmapDisplayer(5)).build();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		// set view for list view item
		LayoutInflater inflater = context.getLayoutInflater();
		convertView = inflater.inflate(resource, null);

		Cyclo cyclo = arr.get(position);
		ImageView cycloImage = (ImageView) convertView
				.findViewById(R.id.cycloImage);
		TextView cycloTitle = (TextView) convertView
				.findViewById(R.id.cycloTitle);
		TextView cycloWebsite = (TextView) convertView
				.findViewById(R.id.cycloWebsite);
		TextView cycloPhoneNumber = (TextView) convertView
				.findViewById(R.id.cycloPhoneNumber);
		// set place icon
		imageLoader.displayImage(arr.get(position).getIconPath(), cycloImage,
				options, null);

		cycloTitle.setText(cyclo.getName());
		cycloWebsite.setText(cyclo.getWebsite());
		cycloPhoneNumber.setText(cyclo.getPhoneNumber());

		return convertView;
	}

}
